/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package z.h.w.jar.kit.clazz.security;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import z.h.w.jar.kit.method.ED;
import z.h.w.jar.kit.method.Roll;
import z.h.w.jar.kit.method.Text;

/**
 * 2012.07.11
 *
 * @author EnzoZhong
 */
public class BlankCode {

      private List<String> codeList;
      private Integer len;

      public BlankCode ( String key , Integer maxLen ) {
            codeList = Text.splitString ( ED.safe ( key ) , maxLen );
            this.len = codeList.size ();
      }

      public Integer[] randomKey ( Integer keyLen ) {
            if ( keyLen > len / 2 ) {
                  throw new RuntimeException ( "请选择数值小于 " + len / 2 );
            } else {
                  Set<Integer> hashSet = new LinkedHashSet<> ( keyLen + 2 );
                  while ( hashSet.size () < keyLen ) {
                        hashSet.add ( Roll.range ( len ) );
                  }
                  Integer[] integer = new Integer[ keyLen ];
                  return hashSet.toArray ( integer );
            }
      }

      public String getPassword ( Integer[] keys ) {
            String re = "";
            for ( Integer key : keys ) {
                  re += codeList.get ( key );
            }
            return re;
      }

}
